package com.haofengsoft.scce.ui.mine;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.haofengsoft.platform.utils.PhoneUtil;
import com.haofengsoft.platform.utils.ToastUtil;
import com.haofengsoft.scce.R;
import com.haofengsoft.scce.common.client.VolleyBean;
import com.haofengsoft.scce.common.client.VolleyInterface;
import com.haofengsoft.scce.common.client.VolleyManage;
import com.haofengsoft.scce.common.config.ConstantConfig;
import com.haofengsoft.scce.db.bean.UserInfo;
import com.haofengsoft.scce.db.cache.UserCache;

public class FeedBackActivity extends Activity {

    private EditText infoEt;
    private EditText telEt;
    private Button submitFeedbackBtn;
    private UserInfo userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feedback);
        userInfo = UserCache.getInstance().getUser(FeedBackActivity.this);
        ImageView feedbackTitleBackIv = (ImageView) findViewById(R.id.feedback_title_back_iv);
        infoEt = (EditText) findViewById(R.id.feedback_info_et);
        telEt = (EditText) findViewById(R.id.feedback_tel_ev);
        if (-1 != userInfo.getId()) {
            telEt.setText(userInfo.getAccount());
        }
        submitFeedbackBtn = (Button) findViewById(R.id.feedback_submit_btn);
        submitFeedbackBtn.setOnClickListener(new SubmitFeedClickListener());
        feedbackTitleBackIv.setOnClickListener(new BackClickListener());
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    class BackClickListener implements OnClickListener {

        @Override
        public void onClick(View view) {
            finish();
        }
    }

    class SubmitFeedClickListener implements OnClickListener {

        @Override
        public void onClick(View view) {
            if (infoEt.getText().toString().trim().equals("")) {
                ToastUtil.TextToast(FeedBackActivity.this, "请输入您的建议或意见",
                        Toast.LENGTH_SHORT);
            } else if (!PhoneUtil.isMobileNO(telEt.getText().toString())) {
                ToastUtil.TextToast(FeedBackActivity.this, "请输入您的联系方式",
                        Toast.LENGTH_SHORT);
            } else {
                Map<String, Object> mapParam = new HashMap<String, Object>();
                mapParam.put("UserID", userInfo.getId());
                mapParam.put("Mobile", telEt.getText().toString().trim());
                mapParam.put("Message", infoEt.getText().toString());
                VolleyManage.getInstance().methodPost(FeedBackActivity.this,
                        "发送中", ConstantConfig.FEEDBACK_URL, mapParam,
                        new FeedBackVolleyReal());
            }
        }

    }

    class FeedBackVolleyReal implements VolleyInterface {

        @Override
        public void gainData(VolleyBean bean) {
            if (bean.isSuccess()) {
                ToastUtil.TextToast(FeedBackActivity.this, "感谢您的建议",
                        Toast.LENGTH_SHORT);
            } else {
                ToastUtil.TextToast(FeedBackActivity.this, "网络连接出错，请稍后再试",
                        Toast.LENGTH_SHORT);
            }
        }
    }

}
